﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NerdDinner.Controllers.components;
using NerdDinner.Models;

namespace NerdDinner.Controllers
{
    public class DinnersController : Controller
    {
        private DinnerRepository dinnerRepository = new DinnerRepository();
        //
        // GET: /Dinners/

        public ActionResult Index(int? page)
        {
            const int pageSize = 10;
            Response.Write(page);
            var tempDinners = dinnerRepository.FindUpcomingDinners();
            var dinners = new PaginatedList<Dinner>(tempDinners,(page ?? 0),pageSize);
            return View("Index", dinners);
        }
        public ActionResult Details(int id)
        {
            var dinner = dinnerRepository.FindDinner(id);
            if (dinner == null)
            {
                return View("NotFound");
            }
            return View("Details", dinner);
        }
        public ActionResult Edit(int id)
        {
            var dinner = dinnerRepository.FindDinner(id);
            return View(dinner);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            var dinner = dinnerRepository.FindDinner(id);
            try
            {
                UpdateModel(dinner);  // UpdateModel() method to simplify our HTTP-POST Edit Action
                dinnerRepository.Save();
                return RedirectToAction("Details", new {id = dinner.DinnerID});
            }
            catch (Exception)
            {

                foreach (var issue in dinner.GetRuleViolations())
                {   
                    /*
                     * "ModelState" property collection which provides a way to indicate that errors exist with a model object being passed to a View.
                     */
                    ModelState.AddModelError(issue.PropertyName,issue.ErrorMessage);
                }
                return View(dinner);
            }
        }
        [Authorize(Roles = "admin")]
        public ActionResult Create()
        {
            var dinner = new Dinner();
            dinner.EventDate = DateTime.Now.AddDays(7);
            return View(dinner);
        }
        [AcceptVerbs(HttpVerbs.Post),Authorize]
        public ActionResult Create(Dinner dinner)
        {
            if (ModelState.IsValid)
            {

                try
                {
                    //UpdateModel(dinner);]
                    dinner.HostedBy = User.Identity.Name;
                    RSVP rsvp = new RSVP();
                    rsvp.AttendeeName = User.Identity.Name;
                    dinner.RSVPs.Add(rsvp);
                    dinnerRepository.Add(dinner);
                    dinnerRepository.Save();

                    return RedirectToAction("Details", new { id = dinner.DinnerID });
                }
                catch
                {
                    ModelState.AddRuleViolations(dinner.GetRuleViolations());
                    return View("NotFound");
                }
            }

            return View(dinner);
        }
        
        public ActionResult Delete(int id)
        {
            Dinner dinner;
            if (( dinner = dinnerRepository.FindDinner(id)) == null)
            {
                return View("NotFound");
            }
            else
            {
                return View(dinner);
            }
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id, string button)
        {
            try
            {
                var dinner = dinnerRepository.FindDinner(id);
                dinnerRepository.Delete(dinner);
                dinnerRepository.Save();
                return View("Deleted");
            }
            catch (Exception)
            {

                return View("NotFound");
            }
           
        }
        

    }
}
